import React from "react";
import BaseComponent from "../../redare/components/BaseComponent";
import Grid from "../../redare/components/grid/Grid";
import {Divider, Space} from "antd";
import GeneratorSysPresenter from "../../.gen/presenter/GeneratorSysPresenter";
import moment from "moment";
import {Link} from "react-router-dom";


export default class GeneratorSysGrid extends BaseComponent {

    constructor(props) {
        super(props);
        this.presenter = new GeneratorSysPresenter(this)
    }

    componentDidMount() {
        this._findGeneratorSysPage()
    }

    //搜索
    _findGeneratorSysPage(searchParams, page) {
        if (this.grid) {
            this.grid.showLoadingIndicator()
        }
        this.searchParams = searchParams;
        if (!this.searchParams) {
            this.searchParams = {}
        }
        if (page) {
            this.curPage = page;
        }
        this.presenter.findGeneratorSysPage(
            this.searchParams,
            {curPage: this.curPage, pageSize: this.pageSize},
            (result) => {
                let {data} = result;
                if (data && this.grid) {
                    let {result, curPage, pageSize, totalCount, totalPage} = data;
                    this.totalCount = totalCount;
                    this.totalPage = totalPage;
                    this.grid.setData(result, {curPage, pageSize, totalCount})
                }
            },
            () => {
                if (this.grid) {
                    this.grid.closeLoadingIndicator()
                }
            }
        )
    }


    //删除
    _delGeneratorSys(id) {
        this.confirm("确定删除吗？", () => {
            this.presenter.deleteGeneratorSys({id: id}, {}, () => {
                this.toastSuccess("删除成功");
                this._findGeneratorSysPage(this.searchParams, 1);
            })
        })
    }

    //批量删除
    _batchDel = () => {
        let {ids} = this;
        if (!ids || ids.length <= 0) {
            this.toastWarning("请选择数据");
            return
        }
        this.confirm("确定删除吗？", () => {
            this.presenter.batchDeleteGeneratorSys({ids: ids}, {},
                () => {
                    this.toastSuccess("批量删除成功");
                    this._findGeneratorSysPage(this.searchParams, 1);
                })
        })
    };


    //分页
    _changePage(curPage, pageSize) {
        this.curPage = curPage;
        this.pageSize = pageSize;
        this._findGeneratorSysPage(this.searchParams)
    }

    render() {
        return (
            <Grid ref={(o) => {
                this.grid = o
            }}
                  border={false}
                  _initLoading={false}
                  scroll={{x: 'max-content'}}
                  bordered={false}
                  rowKey={(v) => v.id}
                  className={"margin-top24"}
                  _onPageChange={(page) => {
                      this._changePage(page.curPage, page.pageSize)
                  }}
                  columns={[
                      {
                          dataIndex: 'code',
                          width: 150,
                          title: '系统编码'
                      },
                      {
                          dataIndex: 'name',
                          width: 150,
                          title: '系统名称'
                      },
                      {
                          title: '创建时间',
                          dataIndex: 'createTime',
                          width: 150,
                          render: (value, record, index) => {
                              return value ? moment(value).format("YYYY-MM-DD HH:mm:ss") : '';
                          }
                      },
                      {
                          title: '操作',
                          width: 180,
                          fixed: 'right',
                          render: (value, record, index) => {
                              return (
                                  <Space size="middle">
                                      <Link to={{
                                          pathname: "/onlformhead/manager",
                                          search: "?id=" + record.id
                                      }}>查看</Link>
                                      <a onClick={()=>{
                                          if (this.props.itemEventListener){
                                              this.props.itemEventListener('edit',record);
                                          }
                                      }}>编辑</a>
                                      <a onClick={this._delGeneratorSys.bind(this, record.id)}>删除</a>
                                  </Space>
                              )
                          }
                      }
                  ]}
            />
        )
    }

}




